import 'dart:math';

// abstract class Shape {
//   num get area;
// }

class Circle implements Shape {
  final num radius;
  Circle(this.radius);
  num get area => pi * pow(radius, 2);
}

class Square implements Shape {
  final num side;
  Square(this.side);
  num get area => pow(side, 2);
}

// 工厂函数
Shape shapeFactory(String type) {
  if (type == 'circle') return Circle(2);
  if (type == 'square') return Square(2);
  throw 'Can\'t create $type.';
}

// 创建工厂函数第二种
// 将工厂构造函数添加到抽象 Shape 类中
abstract class Shape {
  factory Shape(String type) {
    if (type == 'circle') return Circle(2);
    if (type == 'square') return Square(2);
    throw 'Can\'t create $type.';
  }
  num get area;
}

main() {
  // final circle = Circle(2);
  // final square = Square(2);

  // final circle = shapeFactory('circle');
  // final square = shapeFactory('square');

  final circle = Shape('circle');
  final square = Shape('square');

  print(circle.area);
  print(square.area);
}

// 添加 CircleMock 类，以实现 Circle 接口：类就是接口
// 建议使用抽象类定义接口
class CircleMock implements Circle {
  num area = 0;
  num radius = 0;
}
